package com.lol.service;

import com.lol.dao.UserMapper;
import com.lol.tool.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lol.pojo.User;
import com.lol.tool.LOLResult;

import javax.annotation.Resource;


@Service
public class UserServiceLmpl implements UserService {

    @Autowired
    private LOLResult<User> result;
    @Autowired
    private User user;
    @Autowired
    private UserMapper userDao;


    public LOLResult<User> checkLogin(String email, String password) throws Exception {
        User checkUser=null;
        try{
            checkUser = userDao.findByName(email);

        }catch (Exception e) {
            System.out.println(e);
            result.setMsg("email not exist");
            return result;
        }

        String chPwd=checkUser.getPassword();
        String md5Password = MD5.md5Encode(password);

        if (chPwd.equals(md5Password)){
            result.setMsg("success");

            return result;
        }

        return null;

    }

    public LOLResult<User> addUser(String email, String password, String nickName) throws Exception {
        try {
            User checkUser = userDao.findByName(email);
            if (checkUser != null) {
                result.setMsg("email was exist");

                return result;
            }
        } catch (Exception e) {
            System.out.println(e);
        }

        String cId = MD5.createId();
        String md5Password = MD5.md5Encode(password);
        user.setUserid(cId);
        user.setUsername(email);
        user.setPassword(md5Password);

        userDao.save(user);
        result.setStatus(4);
        result.setMsg("注册成功");

        return result;


    }
}
